﻿using MES_SystemUI.Helpers;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

// MES_SystemUI.Helpers.VerifyCode
namespace MES_SystemUI.Helpers
{
    public static class VerifyCode
    {
        // 验证 随机字母数字
        /// <summary>
        /// 随机生成【A-Z,a-z,0-9】字符串
        /// </summary>
        /// <param name="count">生成字符串长度</param>
        /// <param name="strBigFlag">是否大写字母</param>
        /// <param name="strSmallFlag">是否小写字符</param>
        /// <param name="strIntFlag">是否数字</param>
        /// <returns>string字符串</returns>
        public static string GetVerifyString(int count, bool strBigFlag = true, bool strSmallFlag = true, bool strIntFlag = true)
        {
            // 62
            // string str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
            string strBig = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
            string strSmall = "abcdefghijklmnopqrstuvwxyz";
            string strInt = "1234567890";
            string str = strBig + strSmall + strInt;
            string strBS = strBig + strSmall;
            string strBI = strBig + strInt;
            string strSI = strSmall + strInt;
            Random r = new Random();
            string Verify = string.Empty;
            for (int i = 0; i < count; i++)
            {
                if (strBigFlag && strSmallFlag && strIntFlag)
                {
                    Verify += str[r.Next(0, str.Length)];
                }
                else if (strBigFlag && strSmallFlag)
                {
                    Verify += strBS[r.Next(0, strBS.Length)];
                }
                else if (strBigFlag && strIntFlag)
                {
                    Verify += strBI[r.Next(0, strBI.Length)];
                }
                else if (strIntFlag && strSmallFlag)
                {
                    Verify += strSI[r.Next(0, strSI.Length)];
                }
                else if (strBigFlag)
                {
                    Verify += strBig[r.Next(0, strBig.Length)];
                }
                else if (strSmallFlag)
                {
                    Verify += strSmall[r.Next(0, strSmall.Length)];
                }
                else if (strIntFlag)
                {
                    Verify += strInt[r.Next(0, strInt.Length)];
                }
            }
            return Verify;
        }
        // 生成验证码 图片
        /// <summary>
        /// 生成验证码所需图片
        /// </summary>
        /// <param name="str">字符串</param>
        /// <param name="Width">图片宽度</param>
        /// <param name="Height">图片高度</param>
        /// <param name="backColorFlag">是否有背景颜色默认true</param>
        /// <param name="fontColorFlag">是否为彩色字，默认true</param>
        /// <param name="shelterFlag">是否生成遮挡，默认true</param>
        /// <returns>返回Image图片</returns>
        public static Image GetVerifyImage(string str, int Width, int Height, bool backColorFlag = true, bool fontColorFlag = true, bool shelterFlag = true)
        {
            Random r = new Random();
            Bitmap bitmap = new Bitmap(Width, Height);
            Graphics g = Graphics.FromImage(bitmap);
            Font font = new Font("宋体", 24F);
            SizeF sizeF = g.MeasureString("A", font);
            //Rectangle rectangle = new Rectangle(0, 0, Width-(int)sizeF.Width, Height-(int)sizeF.Height);
            Rectangle rectangle = new Rectangle(0, 0, Width, Height - (int)sizeF.Height);
            Rectangle Backrectangle = new Rectangle(0, 0, Width, Height);
            if (backColorFlag)
            {
                Color Backcolor = Color.FromArgb(r.Next(0, 100), r.Next(0, 255), r.Next(0, 255), r.Next(0, 255));
                g.FillRectangle(new SolidBrush(Backcolor), Backrectangle);
            }
            int ToWidth = 0;
            for (int i = 0; i < str.Length; i++)
            {
                Color Fontcolor = Color.Black;
                if (fontColorFlag)
                {
                    Fontcolor = Color.FromArgb(r.Next(200, 255), r.Next(0, 255), r.Next(0, 255), r.Next(0, 255));
                }
                int x = r.Next(ToWidth, rectangle.Width - (int)sizeF.Width * (str.Length - i));
                ToWidth = x + (int)sizeF.Width;
                int y = r.Next(0, rectangle.Height);
                g.DrawString(str[i].ToString(), font, new SolidBrush(Fontcolor), x, y);
            }
            if (shelterFlag)
            {
                int shelterCount = r.Next(10, 30);
                for (int i = 0; i < shelterCount; i++)
                {
                    Color shelterColor = Color.FromArgb(r.Next(0, 255), r.Next(0, 255), r.Next(0, 255), r.Next(0, 255));
                    Point point1 = new Point(r.Next(0, Width), r.Next(0, Height));
                    Point point2 = new Point(r.Next(0, Width), r.Next(0, Height));
                    g.DrawLine(new Pen(shelterColor, r.Next(1, 10)) { DashStyle = System.Drawing.Drawing2D.DashStyle.DashDotDot }, point1, point2);
                }
            }
            return bitmap;
        }
    }
}
